# -*- coding: utf-8 -*-
import pymongo
import utils
from scrapy.utils.project import get_project_settings
from itemadapter import ItemAdapter

class IpPoolPipeline(object):
    def __init__(self):
        url, db_name, sheet_name = utils.generate_mongo_conn()
        # 创建MONGODB数据库链接
        self.client = pymongo.MongoClient(url)
        # 指定数据库
        self.db = self.client[db_name]
        # 存放数据的数据库表名
        self.post = self.db[sheet_name]
    
    def close_spider(self, spider):
        self.client.close()

    def process_item(self, item, spider):
        self.post.insert_one(ItemAdapter(item).asdict())
        return item